package com.wkq.common.glide;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.wkq.common.glide.disk.VoiceDiskCacheFactory;
import com.wkq.common.glide.net.OkHttpGlideUrlLoader;
import com.wkq.common.glide.net.ProgressInterceptor;
import okhttp3.Call;
import okhttp3.OkHttpClient;

import java.io.File;
import java.io.InputStream;


/**
 * @Author: wkq
 * @Time: 2025/4/10 15:16
 * @Desc:
 */
@GlideModule
public class VoiceGlideApp extends AppGlideModule{

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        builder.setDiskCache(new VoiceDiskCacheFactory(new VoiceDiskCacheFactory.CacheDirectoryGetter() {
            @NonNull
            @Override
            public File getCacheDirectory() {
                return new File(context.getCacheDir(), GlideConstant.INSTANCE.getDir());
            }
        }));
    }
    public boolean isManifestParsingEnabled() {
        return false;
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new ProgressInterceptor())
                .build();

        registry.replace(
                GlideUrl.class,
                InputStream.class,
                new com.wkq.common.glide.net.OkHttpGlideUrlLoader.Factory((Call.Factory) okHttpClient)
        );

    }
}
